﻿using UnityEngine;

namespace Framework.Utils
{
    public class CoordinateUtil
    {
        public static Vector3 WorldToUISpace(Canvas parentCanvas, Vector3 worldPos)
        {
            //Convert the world for screen point so that it can be used with ScreenPointToLocalPointInRectangle function
            Vector3 screenPos = Camera.main.WorldToScreenPoint(worldPos);
            Vector2 movePos;

            //Convert the screenpoint to ui rectangle local point
            RectTransformUtility.ScreenPointToLocalPointInRectangle(parentCanvas.transform as RectTransform, screenPos, parentCanvas.worldCamera, out movePos);
            //Convert the local point to world point
            // Vector3 pos = parentCanvas.transform.TransformPoint(movePos);
            return movePos;
        }

        public static Vector2 ScreenToUISpace(Canvas parentCanvas, Vector3 screenPos)
        {
            Vector2 movePos;
            RectTransformUtility.ScreenPointToLocalPointInRectangle(parentCanvas.transform as RectTransform, screenPos,
                parentCanvas.worldCamera, out movePos);
            return movePos;
        }
    }
}